package com.k8.registry.util;

import com.k8.rpc.config.RegistryConfig;
import com.k8.rpc.url.URL;

import java.util.Map;

import static com.k8.common.constants.SpiConstants.DEFAULT_REGISTRY;

public class RegistryUtil {
    public static URL buildUrlByRegistryConfig(RegistryConfig registryConfig, URL systemConfigUrl, String side) {
        URL registryConfigURL = new URL();
        registryConfigURL.setPassWord(registryConfig.getPassword());
        registryConfigURL.setUserName(registryConfig.getUserName());
        Map<String, String> anotherParameters = registryConfig.getAnotherParameters();
        if (anotherParameters != null) {
            for (String key : anotherParameters.keySet()) {
                registryConfigURL.setParameter(key, anotherParameters.get(key));
            }
        }
        //registryConfigURL负责配置中心的某些配置，如配置中心的ip端口等
        //后面需要协议头转换，但是对应的注册中心的address那些不需要改变
        //这个应该改变，这个应该是自身的ip等
        registryConfigURL.setProtocol(DEFAULT_REGISTRY);
        registryConfigURL.setSide(side);
        registryConfigURL.setHost(registryConfig.getHost());
        registryConfigURL.setPort(registryConfig.getPort());
        registryConfigURL.setRegistryName(registryConfig.getRegistryName());
        //优先自身的配置，比如从RegistryConfig中获取的Host等
        registryConfigURL.setSystem_first(false);
//        registryConfigURL.putAttribute(REGISTRY_KEY,registryConfigURL);
        registryConfigURL.setSystemConfigUrl(systemConfigUrl);
        //如此返回参数，优先以registryConfigURL为主，并且不影响直接使用urlParam将额外的配置进行导出
        return registryConfigURL;
    }
}
